package com.sdry.web.controller.cb;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.model.cb.DicEntity;
import com.sdry.model.cb.HydroPower;
import com.sdry.model.cb.TableDataInfo;
import com.sdry.model.lz.Customer;
import com.sdry.model.lz.Warehouse;
import com.sdry.service.cb.HydroPowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.List;

/**
 * 房租水电
 * @author cb
 * @2019年9月25日16:00:09
 */
@RequestMapping("hydroPower")
@Controller
public class HydroPowerController extends BaseController{

    @Autowired
    private HydroPowerService hydroPowerService;



    @RequestMapping()
    public String index(Model model) {
        //数据字典
        List<DicEntity> detail = iDicMapper.getNameByType("sys_ware_layer");
        model.addAttribute("dicEntity",detail);
        //仓库
        List<Warehouse> warehouseList = iDicMapper.getWarehouseList();
        model.addAttribute("warehouseList",warehouseList);
        //客户
        List<Customer> customerList = iDicMapper.getCustomerList();
        model.addAttribute("customerList",customerList);
        return "cb/hydroPower";
    }


    @RequestMapping("list")
    @ResponseBody
    public TableDataInfo list(
            @RequestParam("page") Integer page ,
            @RequestParam("pageSize") Integer pageSize,
            String date,
            Long stockId
    ) {
        logger.info("水电房租列表");
        PageHelper.startPage(page,pageSize);
        List<HydroPower> list = hydroPowerService.getList(stockId, date);
        PageInfo pageInfo = new PageInfo(list,5);
        return getDataTable(pageInfo.getList(),Math.toIntExact(pageInfo.getTotal()));
    }

    @RequestMapping("add")
    @ResponseBody
    public Integer add(HydroPower hydroPower) throws ParseException {
        return hydroPowerService.add(hydroPower);
    }

    @RequestMapping("del")
    @ResponseBody
    public Integer del (@RequestParam("idArr[]") String[] idArr) {
        return hydroPowerService.delete(idArr);
    }
}
